package com.m.job.server.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.client.job.core.constant.JobInstanceStatus;
import com.client.job.core.constant.JobZoneEnums;
import com.client.job.core.model.MJob;
import com.client.job.core.model.MJobInstance;
import com.client.job.utils.DataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.m.job.server.model.MJobInstanceDto;
import com.m.job.server.service.MJobInstanceService;
import com.m.job.server.service.MJobService;
import com.m.utils.PageSearchDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/server/job")
@Api(tags = "Job列表")
public class MjobController {

	@Autowired
	private MJobService mJobService;

	@Autowired
	private MJobInstanceService jobInstanceService;
	
	@PostMapping("/zoneMethods")
	@ApiOperation("查询分片策略")
	public DataResult getZoneMethods() {
		List<Map<String, Object>> list = new ArrayList<>();
		for(JobZoneEnums jz : JobZoneEnums.getZoneByCode(null)) {
			Map<String, Object> map = new HashMap<>();
			map.put("key", jz.getCode());
			map.put("value", jz.getMsg());
			list.add(map);
		}
		return DataResult.buildSuccess(list);
	}
	

	@PostMapping("/list")
	@ApiOperation("查询Job列表")
	public DataResult list(@RequestBody PageSearchDomain<MJob> pageSearch) throws Exception {
		PageHelper.startPage(pageSearch.getPageNum(), pageSearch.getPageSize());
		PageInfo<MJob> page = new PageInfo<>(mJobService.findJobDetailList(pageSearch.getObj()));
		return DataResult.buildSuccess(page);
	}

	@PostMapping("/ipPortLook")
	@ApiOperation("节点查询")
	public DataResult ipPortLook(@RequestBody  MJob mJob) {
		MJobInstanceDto mJobInstance = new MJobInstanceDto();
		mJobInstance.setJobId(mJob.getJobId());
		mJobInstance.setServerStatus(JobInstanceStatus.SERVER_RUNNING.getStatus());
		List<MJobInstanceDto> list = jobInstanceService.findJobMachineList(mJobInstance);
		Set<String> nodeMsgSet = new TreeSet<>();
		for (MJobInstance mi : list) {			
			nodeMsgSet.add(mi.getMachineIp() + ":" + mi.getPort() + mi.getBasePath());
		}
		return DataResult.buildSuccess(nodeMsgSet);
	}

	@PostMapping("/addNewJob")
	@ApiOperation("新增Job")
	public DataResult addNewJob(@RequestBody MJob mJob) {
		String checkMsg = mJobService.checkUnion(mJob);
		if (checkMsg != null) {
			return DataResult.buildFail(checkMsg);
		}
		mJobService.addJobDetail(mJob);
		return DataResult.buildSuccess();
	}

	@PostMapping("/updMjob")
	@ApiOperation("修改Job")
	public DataResult updMjob(@RequestBody MJob mJob) {
		int  count =   mJobService.modifyJobDetail(mJob);
		if(count == 0) {
			return DataResult.buildFail("数据无变化");
		}
		return DataResult.buildSuccess();
	}

	@PostMapping("/stopMjob")
	@ApiOperation("停止所有实例")
	public DataResult stopMjob(String jobId) throws Exception {
		MJobInstanceDto jobInstance = new MJobInstanceDto();
		jobInstance.setJobId(jobId);
		jobInstance.setJobStatus(JobInstanceStatus.JOB_RUNNING.getStatus());
		jobInstance.setServerStatus(JobInstanceStatus.SERVER_RUNNING.getStatus());
		List<MJobInstanceDto>  list = jobInstanceService.findJobMachineList(jobInstance);
		if(list != null && !list.isEmpty()) {
			jobInstanceService.stopJobInstance(list.stream().map(MJobInstance::getMachineId).collect(Collectors.toList()));
		}
		return DataResult.buildSuccess(list.size());
	}
	
	@PostMapping("/startMjob")
	@ApiOperation("开启所有实例")
	public DataResult startMjob(String jobId)throws Exception {
		MJobInstanceDto jobInstance = new MJobInstanceDto();
		jobInstance.setJobId(jobId);
		jobInstance.setJobStatus(JobInstanceStatus.JOB_STOPING.getStatus());
		jobInstance.setServerStatus(JobInstanceStatus.SERVER_RUNNING.getStatus());
		List<MJobInstanceDto>  list = jobInstanceService.findJobMachineList(jobInstance);
		if(list != null && !list.isEmpty()) {
			jobInstanceService.startJobInstance(list.stream().map(MJobInstance::getMachineId).collect(Collectors.toList()));
		}
		return DataResult.buildSuccess(list.size());
	}
	
	
	
	
	
	

}
